package com.project.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.RoleBean;
import com.project.mapper.IRoleMapper;
import com.project.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.management.relation.Role;
import java.util.List;

@Transactional
@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private IRoleMapper roleMapper;

    @Override
    public IPage<RoleBean> cutAll(int pageNO) {
        return roleMapper.selectPage(new Page<RoleBean>(pageNO,PAGE_SIZE),null);
    }

    @Override
    public List<RoleBean> findAll() {
        return roleMapper.selectList(null);
    }

    @Override
    public void add(RoleBean role, int[] gradeArray) {
        roleMapper.insert(role);
        roleMapper.addGradeList(role.getId(),gradeArray);
    }

    @Override
    public RoleBean findById(int id) {
        RoleBean roleBean = roleMapper.selectById(id);
        roleBean.setGradeList(roleMapper.findByRoleId(id));
        return roleBean;
    }

    @Override
    public void updateGrade(int roleId, int[] gradeArray) {
        roleMapper.updateGrade(roleId,gradeArray);
    }

    @Override
    public void del(int roleId) {
        roleMapper.del(roleId);
    }
}
